package com.p2p.backend.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.p2p.backend.command.UserCommand;
import com.p2p.backend.dao.BackUserDao;
import com.p2p.backend.dao.PrivilegeDao;
import com.p2p.backend.model.BackUser;
import com.p2p.backend.model.Privilege;
import com.p2p.backend.service.BackUserService;
import com.p2p.base.page.PageVo;
import com.p2p.base.util.MD5Util;

@Service("backUserService")
public class BackUserServiceImpl implements BackUserService {
	
	@Resource
	private BackUserDao backUserDao;
	
	@Autowired
	private PrivilegeDao privilegeDao;
	
	@Override
	public BackUser checkLogin(BackUser user) {
		BackUser dbMember = backUserDao.findByLoginName(user.getLoginName());
		if (dbMember == null){
			return null;
		}else{
			if (dbMember.getPassword().equals(MD5Util.sha1(user.getPassword()))){
				return dbMember;
			}else{
				return null;
			}
		}
	}

	@Override
	public List<Privilege> findPrivilegeByUserId(Long userId) {
		List<Privilege> list = backUserDao.findPrivilegeByUserId(userId);
		return list;
	}

	@Override
	public List<Map<String, Object>> findAllByUserId(Long userId) {
		List<Map<String, Object>> list =null;
		if(userId.equals(1L)){
			list=privilegeDao.listAllMenu();
		}else{
			 list=privilegeDao.findAllByUserId(userId);
		}
		return list;
	}
	
	@Override
	public String passwordModify(UserCommand userCommand, Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean add(Map<String, Object> user) {
		try {
			backUserDao.add(user);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}

	@Override
	public boolean update(Map<String, Object> user) {
		try {
			backUserDao.update(user);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public PageVo list(PageVo page) {
		List list=backUserDao.listPage(page);
		page.setList(list);
		return page;
	}

	@Override
	public Map<String, Object> get(Integer uid) {
		return backUserDao.get(uid);
	}


}
